package com.wdf.project03.controller;


import com.wdf.project03.pojo.Emp;
import com.wdf.project03.pojo.Result;
import com.wdf.project03.service.EempService;
import com.wdf.project03.util.JwtUtils;
import jdk.nashorn.internal.ir.ReturnNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EempService empService;
    @PostMapping("login")
    public Result login(@RequestBody Emp emp){
        log.info("员工登录：{}",emp);
        Emp empQuery = empService.selectCount(emp);
        if(empQuery != null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",empQuery.getId());
            claims.put("name",empQuery.getName());
            claims.put("username",empQuery.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }

}
